package com.ningxun.pagent.web.controller.bus;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ningxun.pagent.bus.service.IBusPolicyApproveService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ningxun.pagent.common.annotation.Log;
import com.ningxun.pagent.common.core.controller.BaseController;
import com.ningxun.pagent.common.core.domain.AjaxResult;
import com.ningxun.pagent.common.enums.BusinessType;
import com.ningxun.pagent.bus.domain.BusPolicyContent;
import com.ningxun.pagent.bus.service.IBusPolicyContentService;
import com.ningxun.pagent.common.utils.poi.ExcelUtil;
import com.ningxun.pagent.common.core.page.TableDataInfo;

/**
 * 政策Controller
 * 
 * @author pagent
 * @date 2025-07-10
 */
@RestController
@RequestMapping
public class BusPolicyContentController extends BaseController {
    @Autowired
    private IBusPolicyContentService busPolicyContentService;

    @Autowired
    private IBusPolicyApproveService busPolicyApproveService;

    /**
     * 查询政策内容列表
     */
    @PreAuthorize("@ss.hasPermi('bus:policy:list')")
    @GetMapping("/bus/policy/list")
    public TableDataInfo list(BusPolicyContent busPolicyContent) {
        startPage();
        List<BusPolicyContent> list = busPolicyContentService.selectBusPolicyContentList(busPolicyContent);
        return getDataTable(list);
    }

    /**
     * 导出政策内容列表
     */
    @PreAuthorize("@ss.hasPermi('bus:policy:export')")
    @Log(title = "政策内容", businessType = BusinessType.EXPORT)
    @PostMapping("/bus/policy/export")
    public void export(HttpServletResponse response, BusPolicyContent busPolicyContent) {
        List<BusPolicyContent> list = busPolicyContentService.selectBusPolicyContentList(busPolicyContent);
        ExcelUtil<BusPolicyContent> util = new ExcelUtil<BusPolicyContent>(BusPolicyContent.class);
        util.exportExcel(response, list, "政策内容数据");
    }

    /**
     * 获取政策内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:policy:query')")
    @GetMapping(value = "/bus/policy/{policyId}")
    public AjaxResult getInfo(@PathVariable("policyId") Long policyId) {
        return success(busPolicyContentService.selectBusPolicyContentByPolicyId(policyId));
    }



    /**
     * 增加政策内容
     */
    @PreAuthorize("@ss.hasPermi('bus:policy:add')")
    @Log(title = "政策内容", businessType = BusinessType.INSERT)
    @PostMapping("/bus/policy")
    public AjaxResult add(@RequestBody BusPolicyContent busPolicyContent) {
        return toAjax(busPolicyContentService.insertBusPolicyContent(busPolicyContent));
    }

    /**
     * 修改政策内容
     */
    @PreAuthorize("@ss.hasPermi('bus:policy:edit')")
    @Log(title = "政策内容", businessType = BusinessType.UPDATE)
    @PutMapping("/bus/policy")
    public AjaxResult edit(@RequestBody BusPolicyContent busPolicyContent) {
        return toAjax(busPolicyContentService.updateBusPolicyContentWithWorkflow(busPolicyContent));
    }

    /**
     * 删除政策内容
     */
    @PreAuthorize("@ss.hasPermi('bus:policy:remove')")
    @Log(title = "政策内容", businessType = BusinessType.DELETE)
    @DeleteMapping("/bus/policy/{policyIds}")
    public AjaxResult remove(@PathVariable Long[] policyIds) {
        return toAjax(busPolicyContentService.deleteBusPolicyContentByPolicyIds(policyIds));
    }

    /**
     * 提交政策审批
     */
    @PreAuthorize("@ss.hasPermi('system:leave:submit')")
    @Log(title = "政策申请", businessType = BusinessType.OTHER)
    @GetMapping(value = "/submit")
    public AjaxResult submit(Long policyId, Long approveStatus) {
        return toAjax(busPolicyContentService.submit(policyId, approveStatus));
    }

    /**
     * 处理政策审批
     */
    @PreAuthorize("@ss.hasPermi('bus:policyApprove:edit')")
    @Log(title = "政策审核", businessType = BusinessType.UPDATE)
    @PostMapping("/bus/policyApprove")
    public AjaxResult edit(@RequestBody BusPolicyContent busPolicyContent, Long taskId,
                           String skipType, String message, String nodeCode, String flowStatus)
    {
        return toAjax(busPolicyContentService.handle(busPolicyContent, skipType, taskId,
                message, nodeCode, flowStatus));
    }

    /**
     * 驳回政策审批
     */
    @PreAuthorize("@ss.hasPermi('bus:policyApprove:edit')")
    @Log(title = "政策审核", businessType = BusinessType.UPDATE)
    @PostMapping("/bus/policyApprove/rejectLast")
    public AjaxResult rejectLast(@RequestBody BusPolicyContent busPolicyContent, Long taskId,
                                 String message, String flowStatus) {
        return toAjax(busPolicyContentService.rejectLast(busPolicyContent, taskId, message, flowStatus));
    }
}
